package com.zeekr.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.zeekr.bean.FaultTypeImportTemplateRes;
import com.zeekr.bean.Result;
import com.zeekr.util.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: e-Yuanping.Yang1
 * @time: 2024/6/17 13:27
 */
@Slf4j
@RestController
@RequestMapping("/excel")
public class ExcelController {
    final String[] exColImportTemplateCn = {"序号", "故障类型code", "故障类型名称", "事件分类", "故障现象", "故障发生原因", "处理建议", "CSC"};
    final String[] exRowImportTemplate = {"num", "code", "name", "eventCategoryCode", "phenomena", "cause", "handleSuggestion", "cscCode"};

    /**
     * excel导出方案一
     * @param response
     * @throws IOException
     *
     */
    @PostMapping("/exportTempOne")
    public Result<String> schemeOne(HttpServletResponse response) throws IOException {
        List<FaultTypeImportTemplateRes> faultTypeImportTemplateResList = new ArrayList<>();
        FaultTypeImportTemplateRes faultTypeImportTemplateRes = new FaultTypeImportTemplateRes();
        faultTypeImportTemplateRes.setNum(1);
        faultTypeImportTemplateRes.setCode("FA00001");
        faultTypeImportTemplateRes.setName("故障类型名称");
        faultTypeImportTemplateRes.setEventCategoryCode("F1 热失控\n" +
                "F2 特定场景事故碰撞\n" +
                "F3 一般事故碰撞\n" +
                "F4 动力类/制动系统类故障");
        faultTypeImportTemplateRes.setPhenomena("异响");
        faultTypeImportTemplateRes.setCause("胶垫老化");
        faultTypeImportTemplateRes.setHandleSuggestion("更换胶垫");
        faultTypeImportTemplateRes.setCscCode("1NS");
        faultTypeImportTemplateResList.add(faultTypeImportTemplateRes);
        ExcelUtils.export(response, UUID.fastUUID().toString(),faultTypeImportTemplateResList,exColImportTemplateCn, exRowImportTemplate);
        return Result.success();
    }
    /**
     * excel导出方案二
     * @param response
     * @throws IOException
     */
    @GetMapping("/exportTempTwo")
    public void schemeTwo(HttpServletResponse response) throws IOException {
        List<FaultTypeImportTemplateRes> faultTypeImportTemplateResList = new ArrayList<>();
        FaultTypeImportTemplateRes faultTypeImportTemplateRes = new FaultTypeImportTemplateRes();
        faultTypeImportTemplateRes.setNum(1);
        faultTypeImportTemplateRes.setCode("FA00001");
        faultTypeImportTemplateRes.setName("故障类型名称");
        faultTypeImportTemplateRes.setEventCategoryCode("F1 热失控");
        faultTypeImportTemplateRes.setPhenomena("异响");
        faultTypeImportTemplateRes.setCause("胶垫老化");
        faultTypeImportTemplateRes.setHandleSuggestion("更换胶垫");
        faultTypeImportTemplateRes.setCscCode("1NS");
        faultTypeImportTemplateResList.add(faultTypeImportTemplateRes);
//        ExcelUtils.export(response, UUID.fastUUID().toString(),faultTypeImportTemplateResList,exColImportTemplateCn, exRowImportTemplate);

        //通过hutool工具创建的excel的writer，默认为xls格式
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义excel标题和列名
        writer.addHeaderAlias("num","序号");
        writer.addHeaderAlias("code","故障类型code");
        writer.addHeaderAlias("name","故障类型名称");
        writer.addHeaderAlias("eventCategoryCode","事件分类");
        writer.addHeaderAlias("phenomena","故障现象");
        writer.addHeaderAlias("cause","故障发生原因");
        writer.addHeaderAlias("handleSuggestion","处理建议");
        writer.addHeaderAlias("cscCode","CSC");
        writer.setColumnWidth(0,20);
        writer.setColumnWidth(1,20);
        writer.setColumnWidth(2,20);
        writer.setColumnWidth(3,20);
        writer.setColumnWidth(4,20);
        writer.setColumnWidth(5,20);
        writer.setColumnWidth(6,20);
        writer.write(faultTypeImportTemplateResList,true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //name是下载对话框的名称，不支持中文，想用中文名称需要进行utf8编码
        String excelName = "故障类型";
        excelName = URLEncoder.encode(excelName, "utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + excelName +".xlsx");
        ServletOutputStream excelOut = null;
        //将excel文件信息写入输出流，返回给调用者
        try {
            excelOut = response.getOutputStream();
            writer.flush(excelOut,true);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            writer.close();
        }
    }
    @PostMapping("/import")
    public void excelImport(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String[] excelHead={"序号","故障类型code","故障类型名称","事件分类","故障现象","故障发生原因","处理建议","CSC"};
        String[] excelHeadAlias={"num","code","name","eventCategoryCode","phenomena","cause","handleSuggestion","cscCode"};
        List<FaultTypeImportTemplateRes> faultTypeImportTemplateRes = ExcelUtils.importExcel(inputStream, excelHead, excelHeadAlias, FaultTypeImportTemplateRes.class);
        log.info("导入数据:{}", JSON.toJSONString(faultTypeImportTemplateRes));
    }
}
